<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <$render "common/css.html"$>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">任务名称</label>
        <div class="layui-input-block">
            <input type="text" name="TaskName" lay-verify="required" lay-reqtext="任务名称不能为空" placeholder="请输入任务名称"
                   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">任务描述</label>
        <div class="layui-input-block">
            <textarea name="TaskDescript" class="layui-textarea" placeholder="请输入任务描述"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">定时时间</label>
        <div class="layui-input-block">
            <input type="text" name="TaskSpec" lay-verify="required" lay-reqtext="定时时间不能为空" placeholder="请输入定时时间"
                   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">任务类型</label>
        <div class="layui-input-block">
            <input type="radio" name="TaskType" value="email" title="email" checked="" lay-filter="taskType">
            <input type="radio" name="TaskType" value="cmd" title="cmd" lay-filter="taskType">
            <input type="radio" name="TaskType" value="test" title="test" lay-filter="taskType">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">所需数据</label>
        <div class="layui-input-block">
            <textarea name="TaskData" class="layui-textarea" placeholder="请输入任务所需数据"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="TaskId" value="">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<$ render "common/js.html" $>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var TaskId = '<$ .taskId $>';
        if (TaskId) {
            $('input[name="TaskId"]').val(TaskId);
            ajaxRequest({"TaskId": TaskId}, '<$ urlpath "taskInfo" $>')
                .then(res => {
                    if (res.code == 0) {
                        let vals = res.data;
                        for (let i in vals) {
                            let val = vals[i]
                            if (i == 'TaskType') {
                                if (val != "") {
                                    $("input[value='" + val + "']").attr('checked', 'true');
                                }

                            } else if (i == 'TaskDescript' || i == 'TaskData') {
                                $('textarea[name="' + i + '"]').val(val);
                            } else {
                                $('input[name="' + i + '"]').val(val);
                            }

                        }
                        form.render();
                    }
                })
                .catch(err => {
                    console.log("error: " + err)
                });
        }
        form.on('radio(taskType)', function (data) {
            console.log(data.elem); //得到radio原始DOM对象
            console.log(data.value); //被点击的radio的value值
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let param = data.field;
            ajaxRequest(param, '<$ urlpath "editTask" $>')
                .then(res => {
                    var index = layer.alert(res.msg, {
                        title: '最终的提交信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                })
                .catch(err => {
                    console.log(err);
                });

            return false;
        });

    });
</script>
</body>
</html>